package co.com.inventarios.persistencia;

import java.util.List;
import java.util.logging.Level;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import co.com.inventarios.modelo.Mngdoc;
import co.com.inventarios.modelo.MngdocId;
import co.com.inventarios.modelo.RequiDoc;
import co.com.inventarios.modelo.RequiDocId;

public class QueryMngdocDAO {

	private EntityManager getEntityManager() {
		return EntityManagerHelper.getEntityManager();
	}

	public List<Mngdoc> findByProperty_list(String clase, String tipo,
			String propertyName, String value, String bodegas) {
		EntityManagerHelper.log("finding RequiDoc instance with id: " + clase,
				Level.INFO, null);
		try {
			getEntityManager().clear();
			
			
			String[] Bod = null;
			String filtro = "";
			Bod = bodegas.split(",");
			for (int i = 0; i < Bod.length; i++) {

				if (i == 0) {
					filtro = filtro + " ( docbodsale='"
							+ Bod[i] + "' ";
				} else {
					filtro = filtro + " OR  docbodsale='" + Bod[i]
							+ "' ";
				}

				if (i == (Bod.length - 1)) {
					filtro = filtro + ")";
				}
			}
			
			if (propertyName == "docnumero") {

				final String queryString = "select model from Mngdoc model where "
						+ filtro
						+ " and model.id.docclase='"
						+ clase
						+ "' and model.id.doctipo='"
						+ tipo
						+ "' and "
						+ propertyName
						+ " = "
						+ Double.valueOf(value)
						+ " ORDER BY model.id.docnumero DESC";
				Query query = getEntityManager().createQuery(queryString);
				return query.getResultList();
			} else {
				final String queryString = "select model from  Mngdoc model where "
						+ filtro
						+ " and model.id.docclase='"
						+ clase
						+ "' and model.id.doctipo='"
						+ tipo
						+ "' and "
						+ propertyName
						+ " = '"
						+ value
						+ "' ORDER BY model.id.docnumero DESC";
				Query query = getEntityManager().createQuery(queryString);
				return query.getResultList();
			}

		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

	public List<Mngdoc> findByProperty_list_Entradas(String clase, String tipo,
			String propertyName, String value, String bodegas) {
		EntityManagerHelper.log("finding RequiDoc instance with id: " + clase,
				Level.INFO, null);
		try {
			getEntityManager().clear();
			
			
			String[] Bod = null;
			String filtro = "";
			Bod = bodegas.split(",");
			for (int i = 0; i < Bod.length; i++) {

				if (i == 0) {
					filtro = filtro + " ( model.bodega.bodcodigo='"
							+ Bod[i] + "' ";
				} else {
					filtro = filtro + " OR  model.bodega.bodcodigo='" + Bod[i]
							+ "' ";
				}

				if (i == (Bod.length - 1)) {
					filtro = filtro + ")";
				}
			}
			
			if (propertyName == "docnumero") {

				final String queryString = "select model from Mngdoc model where "
						+ filtro
						+ " and model.id.docclase='"
						+ clase
						+ "' and model.id.doctipo='"
						+ tipo
						+ "' and "
						+ propertyName
						+ " = "
						+ Double.valueOf(value)
						+ " ORDER BY model.id.docnumero DESC";
				Query query = getEntityManager().createQuery(queryString);
				return query.getResultList();
			} else {
				final String queryString = "select model from  Mngdoc model where "
						+ filtro
						+ " and model.id.docclase='"
						+ clase
						+ "' and model.id.doctipo='"
						+ tipo
						+ "' and "
						+ propertyName
						+ " = '"
						+ value
						+ "' ORDER BY model.id.docnumero DESC";
				Query query = getEntityManager().createQuery(queryString);
				return query.getResultList();
			}

		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}
	
	public Mngdoc findByProperty(String propertyName, MngdocId value) {
		EntityManagerHelper.log("finding Mngdoc instance with property: "
				+ propertyName + ", value: " + value, Level.INFO, null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Mngdoc  model where model."
					+ propertyName
					+ "= :propertyValue  ORDER BY model.id.docnumero DESC ";
			Query query = getEntityManager().createQuery(queryString);
			query.setParameter("propertyValue", value);
			return (Mngdoc) query.getSingleResult();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

	public List<Mngdoc> findById_list(String clase, String tipo, String value,
			String bodegas) {
		EntityManagerHelper.log("finding RequiDoc instance with id: " + clase,
				Level.INFO, null);
		try {
			
			String[] Bod = null;
			String filtro = "";
			Bod = bodegas.split(",");
			for (int i = 0; i < Bod.length; i++) {

				if (i == 0) {
					filtro = filtro + " and ( docbodsale='"
							+ Bod[i] + "' ";
				} else {
					filtro = filtro + " OR  docbodsale='" + Bod[i]
							+ "' ";
				}

				if (i == (Bod.length - 1)) {
					filtro = filtro + ")";
				}
			}
			
			getEntityManager().clear();
			final String queryString = "select model from Mngdoc model where "
					+ " model.id.docclase='"
					+ clase
					+ "' and model.id.doctipo='"
					+ tipo
					+ "'  "
					+ value + "   "
					+ filtro
					+ " ORDER BY model.id.docnumero DESC";
			Query query = getEntityManager().createQuery(queryString);
			return query.getResultList();

		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

	
	public List<Mngdoc> findById_list_Entradas(String clase, String tipo, String value,
			String bodegas) {
		EntityManagerHelper.log("finding RequiDoc instance with id: " + clase,
				Level.INFO, null);
		try {
			
			String[] Bod = null;
			String filtro = "";
			Bod = bodegas.split(",");
			for (int i = 0; i < Bod.length; i++) {

				if (i == 0) {
					filtro = filtro + " and ( model.bodega.bodcodigo='"
							+ Bod[i] + "' ";
				} else {
					filtro = filtro + " OR  model.bodega.bodcodigo='" + Bod[i]
							+ "' ";
				}

				if (i == (Bod.length - 1)) {
					filtro = filtro + ")";
				}
			}
			
			getEntityManager().clear();
			final String queryString = "select model from Mngdoc model where "
					+ " model.id.docclase='"
					+ clase
					+ "' and model.id.doctipo='"
					+ tipo
					+ "'  "
					+ value + "   "
					+ filtro
					+ " ORDER BY model.id.docnumero DESC";
			Query query = getEntityManager().createQuery(queryString);
			return query.getResultList();

		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}
	
	public List<Mngdoc> findByProperty_list_confirmar(String propertyName,
			String value, int estado, String bodegas) {

		try {

			String[] Bod = null;
			String filtro = "";
			Bod = bodegas.split(",");
			for (int i = 0; i < Bod.length; i++) {

				if (i == 0) {
					filtro = filtro + " AND ( model.bodega.bodcodigo='"
							+ Bod[i] + "' ";
				} else {
					filtro = filtro + " OR  model.bodega.bodcodigo='" + Bod[i]
							+ "' ";
				}

				if (i == (Bod.length - 1)) {
					filtro = filtro + ")";
				}
			}

			getEntityManager().clear();
			if (propertyName == "docnumero") {
				final String queryString = "select model from Mngdoc model where model.label.lbcodnum="
						+ estado
						+ " and "
						+ propertyName
						+ " = "
						+ Double.valueOf(value)
						+ " "
						+ filtro
						+ " ORDER BY model.id.docnumero DESC";
				Query query = getEntityManager().createQuery(queryString);
				return query.getResultList();
			} else {
				if (propertyName == "model.label.lbcodnum") {
					final String queryString = "select model from Mngdoc model where "
							+ propertyName
							+ " = "
							+ Double.valueOf(value)
							+ " "
							+ filtro
							+ "ORDER BY model.id.docnumero DESC";
					Query query = getEntityManager().createQuery(queryString);
					return query.getResultList();
				} else {
					final String queryString = "select model from Mngdoc model where model.label.lbcodnum="
							+ estado
							+ " and "
							+ propertyName
							+ " = '"
							+ value
							+ "' " + filtro + " ORDER BY model.id.docnumero DESC";
					Query query = getEntityManager().createQuery(queryString);
					return query.getResultList();
				}
			}

		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

}
